#define _CRT_SECURE_NO_WARNINGS 1

class Solution {
public:
    int maxDistance(vector<vector<int>>& arrays) {
        int n = arrays.size();
        int premax = INT_MIN / 2, premin = INT_MAX / 2;
        int res = 0;
        for (int i = 0; i < n; i++)
        {
            res = max(res, max(premax - arrays[i][0], arrays[i].back() - premin));
            premax = max(premax, arrays[i].back());
            premin = min(premin, arrays[i][0]);
        }
        return res;
    }
};